package com.example.italkapp2.controller.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.italkapp2.R;
import com.example.italkapp2.controller.activity.ChatActivity;
import com.example.italkapp2.model.Model;
import com.example.italkapp2.model.bean.UserInfo;
import com.example.italkapp2.utils.glide.GlideApp;
import com.example.italkapp2.utils.myParse;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.constants.EaseConstant;
import com.hyphenate.easeui.domain.EaseUser;

import java.util.ArrayList;
import java.util.List;

public class ContactAdapter extends BaseAdapter {

    private Context mContext;
    private OnContactListener mContactListener;
    private UserInfo userInfo;
    private List<UserInfo> mUserInfos=new ArrayList<>();

    public ContactAdapter(Context context, ContactAdapter.OnContactListener onContactListener) {
        //获取该适配器所在的页面
        mContext=context;
        mContactListener=onContactListener;
    }

    //刷新数据的方法
    public void refresh(List<UserInfo> userInfos){
        if(userInfos!=null && userInfos.size()>=0){
            //每次有新联系人传入时都要清空集合
            mUserInfos.clear();
            //再一次性把信息全部加载到listView中
            mUserInfos.addAll(userInfos);

            //刷新页面，属于适配器的方法
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return mUserInfos == null ? 0:mUserInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return mUserInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //获取或创建viewHolder
        ContactAdapter.ViewHolder holder=null;
        if (convertView==null){//没有的话就创建viewHolder
            holder=new ContactAdapter.ViewHolder();

            convertView=View.inflate(mContext, R.layout.item_contact,null);
            holder.contact_photo = convertView.findViewById(R.id.contact_photo);
            holder.name=convertView.findViewById(R.id.tv_contact_name);
            holder.ll_contact_friend=convertView.findViewById(R.id.ll_contact_friend);

            convertView.setTag(holder);
        }else{//有的话就获取viewHolder
            holder= (ContactAdapter.ViewHolder) convertView.getTag();
        }

        //获取当前item数据
        userInfo = mUserInfos.get(position);

        //显示当前item数据
        UserInfo user = userInfo;

        if(user!=null){//是联系人的邀请信息
            //展示名称
            holder.name.setText(userInfo.getName());
            String url;
            String hxid = userInfo.getHxid();
            if (hxid.charAt(0) < '0' || hxid.charAt(0) > '9') {
                url = "https://picsum.photos/id/" +
                        myParse.getInstance().paraseIntInRange(hxid, 1000) + "/200";
            } else {
                url = "https://picsum.photos/id/" + hxid + "/200";
            }
            GlideApp.with(mContext).load(url)
                    .defaultContact()
                    .into(holder.contact_photo);

            //展示最后一条聊天记录（如果要写的话）

            //点击该联系人条目的处理,作为一个接口的实现放到了ContactListFragment中
            holder.ll_contact_friend.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mContactListener.onLinerLayoutClick(user);
                    Log.e("ContactAdapter", "点击了" + user.getHxid());
                    //跳转到相应消息页面，要记得写
                    Intent intent = new Intent(mContext, ChatActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("titleName",user.getHxid());
                    bundle.putString(EaseConstant.EXTRA_CONVERSATION_ID, user.getHxid());
                    bundle.putInt(EaseConstant.EXTRA_CHAT_TYPE,EaseConstant.CHATTYPE_SINGLE);
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);


                    /*EaseUser item = contactLayout.getContactList().getItem(position);
                    ContactDetailActivity.actionStart(mContext, item);
                    Intent intent1 = new Intent(mContext, ContactDetailActivity.class);
                    intent.putExtra("user", (EaseUser)user);
                    if(user.getContact() == 0){
                        intent.putExtra("isFriend", true);
                    }else{
                        intent.putExtra("isFriend", false);
                    }

                    context.startActivity(intent);*/
                }
            });

        }else{//是群组的邀请信息

        }

        //返回view
        return convertView;
    }

    private class ViewHolder {
        private TextView name;
        private LinearLayout ll_contact_friend;
        private ImageView contact_photo;
    }

    public interface OnContactListener {
        //点击该联系人
        public void onLinerLayoutClick(UserInfo userInfo);
    }
}
